package com.huluobo.a2301mdmdemo.demo

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.view.MotionEvent
import android.widget.ImageView
import android.widget.RelativeLayout
import com.huluobo.a2301mdmdemo.R
import kotlin.math.roundToInt

/**
 *  Created by LC on 2025/7/7.
 */
class LikeView(context: Context) : RelativeLayout(context) {

    // xy 分别代表手指触摸的坐标是
    fun start(x: Float, y: Float) {
        val iv = ImageView(context)
        iv.setImageResource(R.drawable.liked)

        val param = LayoutParams(100, 100)
        param.leftMargin = x.roundToInt() - 50
        param.topMargin = y.roundToInt() - 50
        iv.layoutParams = param

        val rotation = ObjectAnimator.ofFloat(iv, "rotation", 0f, 20f, 0f, -20f, 0f)
        val alpha = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0f)
        val scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 2f)
        val scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 2f)

        val set = AnimatorSet()
        set.playTogether(alpha, rotation, scaleX, scaleY)
        set.duration = 1000
        set.start()
        set.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                removeView(iv)
            }
        })

        addView(iv)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x = event?.x
        val y = event?.y

        if (event?.action == MotionEvent.ACTION_DOWN) {
            start(x!!, y!!)
        }
        return true
    }
}